package com.peking.donations.plugins.pay.service.impl;

import com.google.common.base.Strings;
import com.peking.donations.constants.ACTCacheNames;
import com.peking.donations.dao.CallOrderDao;
import com.peking.donations.dao.DonationSavingDao;
import com.peking.donations.dao.DonationUserDao;
import com.peking.donations.model.DonationSaving;
import com.peking.donations.model.DonationUser;
import com.peking.donations.plugins.pay.service.NotifyCallBackService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by YGR on 16/3/9.
 */
@Service
@Transactional(readOnly = true)
public class NotifyCallBackServiceImpl implements NotifyCallBackService {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    DonationSavingDao savingDao;

    @Autowired
    DonationUserDao userDao;

    @Autowired
    CallOrderDao callOrderDao;

    @Autowired
    StringRedisTemplate redisTemplate;

    @Override
    public boolean checkOrder(String orderNo, int amount) {
        DonationSaving orderSaving = this.savingDao.findByTransactionId(orderNo);
        if (null != orderSaving) {
            return true;
        }
        return false;
    }

    @Transactional(readOnly = false)
    @Override
    public void updateOrderStateSuccess(String orderNo, String thirdOrderNo) {
        logger.debug("updateOrderStateSuccess {}",orderNo);
        DonationSaving orderSaving = this.savingDao.findByTransactionId(orderNo);
        orderSaving.setFromTransactionId(thirdOrderNo);
        orderSaving.setSavingStatus(DonationSaving.SAVING_STATUS_SUCCESS);
        this.savingDao.save(orderSaving);

        // 更新用户姓名
        DonationUser user = userDao.findOne(orderSaving.getUserId());
        if (!Strings.isNullOrEmpty(user.getRealName())) {
            user.setRealName(orderSaving.getRealName());
            userDao.save(user);
        }

        // 收到红包总份数
        redisTemplate.opsForHash().increment(ACTCacheNames.ACT_GlOAB_STATISTIC.name(), "giftCount",
                orderSaving.getShare());

        if (DonationSaving.SAVING_TYPE_CALLORDER == orderSaving.getType()) {
            // 更新总排行
            redisTemplate.opsForZSet().incrementScore(ACTCacheNames.CALl_ORDER_TOTAL.name(), orderSaving.getOrderId()+"",
                    orderSaving.getShare());

            // 更新召集令内部排行
            redisTemplate.opsForZSet().incrementScore(
                    ACTCacheNames.OWNER_CALL_ORDER_MEMBERS.name() + "_" + orderSaving.getOrderId(),
                    orderSaving.getUserId()+"", orderSaving.getShare());
        }


        if(user.getType()==DonationUser.TYPE_XY&&!Strings.isNullOrEmpty(user.getGrade())){
            redisTemplate.opsForHash().increment(ACTCacheNames.ACT_GRADE_ORDER.name(), user.getGrade() + "_" + user.getDepartmentCode(), 1);
            String order = (String) redisTemplate.opsForHash().get(ACTCacheNames.ACT_GRADE_ORDER.name(), user.getGrade() + "_" + user.getDepartmentCode());
            redisTemplate.opsForHash().put(ACTCacheNames.ACT_GRADE_ORDER.name(), user.getGrade() + "_" + user.getDepartmentCode()+"_userId",order);

        }

        logger.debug("updateOrderStateSuccess {} success",orderNo);
    }

}
